{% load static %}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入核心的css文件 -->
    <link href="{% static "layui/css/layui.css" %}" rel="stylesheet" media="all">

    <title>Document</title>
    <style>
        .layui-form-label {
            width: 120px !important;
            text-align: center !important;
        }

        .layui-input {
            width: 300px;
        }

        .layui-input-block {
            width: 500px;
        }


    </style>

</head>
<body>

<div>
    <form class="layui-form" action="" lay-filler="event" id="dataFrm">
        {% csrf_token %}
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">显示名称:</label>
                <div class="layui-input-inline">
                    <input type="text" lay-verify="required" name="name" required="required" autocomplete="off"
                           class="layui-input name">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">远程用户名:</label>
                <div class="layui-input-inline">
                    <input type="text" lay-verify="required" name="username" required="required" autocomplete="off"
                           class="layui-input username">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">远程用户密码:</label>
                <div class="layui-input-inline">
                    <input type="password" lay-verify="required" name="password" required="required" autocomplete="off"
                           class="layui-input password">
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button"
                        class="layui-btn layui-btn-lg layui-btn-radius layui-btn-normal layui-icon layui-icon-ok"
                        lay-submit lay-filter="doSubmit">添加
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-radius layui-btn-warm layui-btn-lg   layui-icon  layui-icon-refresh">
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

{% csrf_token %}

<!-- 引入核心的js文件-->
<script src="{% static "layui/layui.js" %}"></script>
<script src="{% static "js/password.js" %}"></script>
<script>

    layui.use(['form', 'layer', 'password'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            password = layui.password



        //监听提交
        form.on("submit(doSubmit)", function (data) {
            // 实际使用时的提交信息
            // console.log(data)


            var name = $('.name').val();

            var username = $('.username').val();
            var password = $('.password').val();

            var csrf_token = $("input[name='csrfmiddlewaretoken']").val();
            // 'csrfmiddlewaretoken': csrf_token  ajex跳转的必须要加上这个
            $.ajax({
                type: "post",
                url: "/addremoteuserclass/",    // 把html输入的数值传给 后边的py代码处理
                data: {
                    "name": name,
                    "username": username,
                    "password": password,
                    'csrfmiddlewaretoken': csrf_token,
                },
                success(res) {
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 6});
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            });
            setTimeout(function () {
                layer.closeAll("addremoteuser.html");
                //刷新父页面
                parent.location.reload();
            }, 1000);
            return false;
        })
    });
</script>
</body>

</html>

